package Thread_;

import java.util.Random;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;

/**
 * CountDownLatch:可同时等待多个线程结束
 * 设同时等待10个线程结束
 * 创建10个线程，同时下载，等待结束
 */
public class Thread27 {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch countDownLatch = new CountDownLatch(10);//10表示：同时等待10个线程结束
        for(int i=0;i<10;i++){ //创建10个线程 负责下载
            int n=i;
            new Thread(()->{
                System.out.println("开始下载： "+n);
                Random random = new Random();
                long s=(random.nextInt(5)+1)*1000;
                try {
                    Thread.sleep(s);//每个线程设置随机休眠时间
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println("结束下载： "+n);
                countDownLatch.countDown();//执行完一个线程，就记录一下
            }).start();
        }
        countDownLatch.await();
        System.out.println("main ");
    }
}
